'
' Effority.Net ( http://www.effority.net )
' Benjamin Hermann, Oliver Dembniak
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Common.Utilities

Namespace effority.Ealo

    Public Class Controller

        Private Shared EALO As String = "effority.Ealo"
        Private Shared SEPERATOR As String = "|"

        Public Shared Function UpdateStringByQualiferAndStringName(ByVal Qualifier As String, ByVal StringName As String, ByVal StringText As String, ByVal Locale As String) As Int32

            Return effority.Ealo.Controller.UpdateStringByQualiferAndStringName(Qualifier, StringName, StringText, Locale, True)

        End Function

        Public Shared Function UpdateStringByQualiferAndStringName(ByVal Qualifier As String, ByVal StringName As String, ByVal StringText As String, ByVal Locale As String, ByVal UpdateCache As Boolean) As Int32

            Dim item As StringInfo = New StringInfo(Qualifier, StringName, StringText, Locale)

            item.Id = effority.Ealo.Data.Controller.UpdateStringByQualiferAndStringNameAndLocale(Qualifier, StringName, StringText, Locale)

            If UpdateCache Then

                If Not item Is Nothing Then

                    effority.Ealo.Utils.StringInfoToCache(item)

                End If

            End If

            Return item.Id

        End Function

        Public Shared Function UpdateStringById(ByVal Id As Int32, ByVal StringText As String)

            Return effority.Ealo.Controller.UpdateStringById(Id, StringText, True)

        End Function

        Public Shared Function UpdateStringById(ByVal Id As Int32, ByVal StringText As String, ByVal UpdateCache As Boolean)

            effority.Ealo.Data.Controller.UpdateStringById(Id, StringText)

            Dim item As StringInfo = effority.Ealo.Controller.GetStringById(Id, False)

            If Not item Is Nothing Then

                If UpdateCache Then

                    If Not item Is Nothing Then

                        effority.Ealo.Utils.StringInfoToCache(item)

                    End If

                End If

                Return item.Id

            Else

                Return Null.NullInteger

            End If

        End Function

        Public Shared Function GetStringById(ByVal Id As Int32) As StringInfo

            Return effority.Ealo.Controller.GetStringById(Id, True)

        End Function

        Public Shared Function GetStringById(ByVal Id As Int32, ByVal FromCache As Boolean) As StringInfo

            Dim CacheKey As String = EALO & SEPERATOR & "Id" & SEPERATOR & Id

            If FromCache Then

                If Not DataCache.GetCache(CacheKey) Is Nothing Then

                    Return CType(DataCache.GetCache(CacheKey), StringInfo)

                Else

                    Dim item As effority.Ealo.StringInfo = Data.Controller.GetStringById(Id)

                    If Not item Is Nothing Then

                        effority.Ealo.Utils.StringInfoToCache(item)

                    End If

                    Return item

                End If

            Else

                ' Wert wir auf jedenfall aus der DB geholt
                ' aber auch trotzdem in den Cache geschrieben

                Dim item As effority.Ealo.StringInfo = Data.Controller.GetStringById(Id)

                If Not item Is Nothing Then

                    effority.Ealo.Utils.StringInfoToCache(item)

                End If

                Return item

            End If

        End Function

        Public Shared Function GetStringByQualifierAndStringName(ByVal Qualifier As String, ByVal StringName As String, ByVal Locale As String) As Ealo.StringInfo
            Return Controller.GetStringByQualifierAndStringName(Qualifier, StringName, Locale, True)
        End Function

        Public Shared Function GetStringByQualifierAndStringName(ByVal Qualifier As String, ByVal StringName As String, ByVal Locale As String, ByVal FromCache As Boolean) As Ealo.StringInfo

            Dim CacheKey As String = EALO & SEPERATOR & Qualifier & SEPERATOR & StringName & SEPERATOR & Locale

            If FromCache Then

                If Not DataCache.GetCache(CacheKey) Is Nothing Then

                    Return CType(DataCache.GetCache(CacheKey), Ealo.StringInfo)

                Else

                    Dim item As Ealo.StringInfo = Data.Controller.GetStringByQualifierAndStringNameAndLocale(Qualifier, StringName, Locale)

                    If Not item Is Nothing Then

                        DataCache.SetCache(CacheKey, item)

                    End If
                    
                    Return item

                End If

            Else

                ' Wert wir auf jedenfall aus der DB geholt
                ' aber auch trotzdem in den Cache geschrieben

                Dim item As Ealo.StringInfo = Data.Controller.GetStringByQualifierAndStringNameAndLocale(Qualifier, StringName, Locale)

                If Not item Is Nothing Then

                    DataCache.SetCache(CacheKey, item)

                End If

                Return item

                End If

        End Function

        Public Shared Function GetStringsByQualifier(ByVal Qualifier As String, ByVal Locale As String) As Generic.Dictionary(Of String, StringInfo)
            Return Controller.GetStringsByQualifier(Qualifier, Locale, True)
        End Function

        Public Shared Function GetStringsByQualifier(ByVal Qualifier As String, ByVal Locale As String, ByVal FromCache As Boolean) As Generic.Dictionary(Of String, StringInfo)

            If String.IsNullOrEmpty(Locale) Or Locale.Length <> 5 Then

                Throw New Exceptions.InvalidLocaleExceptions(Locale)

            End If

            Dim CacheKey As String = EALO & SEPERATOR & Qualifier & SEPERATOR & Locale

            If FromCache Then

                If Not DataCache.GetCache(CacheKey) Is Nothing Then

                    Return CType(DataCache.GetCache(CacheKey), Generic.Dictionary(Of String, StringInfo))

                Else

                    Dim dic As Generic.Dictionary(Of String, StringInfo) = Data.Controller.GetStringsByQualifierAndLocale(Qualifier, Locale)

                    If Not dic Is Nothing Then

                        DataCache.SetCache(CacheKey, dic)

                    End If

                    Return dic

                End If

            Else

                ' Wert wird auf jedenfall aus der DB geholt
                ' aber auch trotzdem in den Cache geschrieben
                Dim dic As Generic.Dictionary(Of String, StringInfo) = Data.Controller.GetStringsByQualifierAndLocale(Qualifier, Locale)

                If Not dic Is Nothing Then

                    DataCache.SetCache(CacheKey, dic)

                End If

                Return dic

            End If

        End Function

        Public Shared Sub DeleteById(ByVal Id As Int32)
            Data.Controller.DeleteById(Id)
        End Sub

        Public Shared Sub DeleteByQualifier(ByVal Qualifier As String)
            Data.Controller.DeleteByQualifier(Qualifier)
        End Sub

        Public Shared Sub DeleteByQualifierAndStringName(ByVal Qualifier As String, ByVal StringName As String)
            Data.Controller.DeleteByQualifierAndStringName(Qualifier, StringName)
        End Sub

        Public Shared Sub DeleteByQualifierAndStringNameAndLocale(ByVal Qualifier As String, ByVal StringName As String, ByVal Locale As String)
            Data.Controller.DeleteByQualifierAndStringNameAndLocale(Qualifier, StringName, Locale)
        End Sub

        Public Shared Sub DeleteByLocale(ByVal Locale As String)
            Data.Controller.DeleteByLocale(Locale)
        End Sub

    End Class

End Namespace